
package oreilly.observe;

import java.util.ArrayList;

/**
 * 天氣主題 <br>
 * Copyright: Copyright (c) 2002-2007<br>
 * Company: E885 有限公司
 * 
 * @author Eric Gau(gau168.gau@msa.hinet.net)
 * @version 1.0
 */
public class WeatherSubject implements Subject
{
	/**
	 * 觀察者集合
	 */
	private java.util.ArrayList observers;

	/**
	 * 溫度
	 */
	private float temperature;

	/**
	 * 溼度
	 */
	private float humidity;

	/**
	 * 壓力
	 */
	private float pressure;

	/**
	 * constructor 產生一個觀察者的集合
	 */
	public WeatherSubject()
	{
		observers = new ArrayList();
	}

	/**
	 * 通知觀察者
	 * 
	 * @see Subject#notifyObservers()
	 */
	public void notifyObservers()
	{
		int size = observers.size();

		for( int i = 0; i < size; i++ )
		{
			( ( Observer )observers.get( i ) ).update( temperature, humidity, pressure );
		}
	}

	/**
	 * @see Subject#registerObserver(Observer)
	 */
	public void registerObserver( Observer observer )
	{
		if( observers.contains( observer ) )
		{
			return;
		}

		observers.add( observer );
	}

	/**
	 * @see Subject#removeObserver(Observer)
	 */
	public void removeObserver( Observer observer )
	{
		int idx;

		if( ( idx = observers.indexOf( observer ) ) != -1 )
		{
			observers.remove( idx );
		}
	}

	/**
	 * 重新設定天氣的數字變化, 溫度, 溼度, 和壓力
	 * 
	 * @param temperature 溫度
	 * @param humidity 溼度
	 * @param pressure 壓力
	 */
	public void setMessurement( float temperature, float humidity, float pressure )
	{
		this.temperature = temperature;
		this.humidity = humidity;
		this.pressure = pressure;
	}
}
